package com.cyy.hardwarecoding.ui.fragment;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cyy.hardwarecoding.R;
import com.cyy.hardwarecoding.ui.adapter.RecyclerViewSimpleAdapter;
import com.cyy.hardwarecoding.ui.adapter.SensorListAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YYCHEN on 2018/1/16.
 */

public class SensorListFragment extends Fragment {

    @BindView(R.id.ls_sensor) RecyclerView ls_sensor;
    private Unbinder unBinder;
    private SensorManager sensorManager;
    private List<Sensor> sensors;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
        sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(getClass().getSimpleName(), "onCreateView method is invoked");
        View root = inflater.inflate(R.layout.fragment_sensor_list, container, false);
        unBinder = ButterKnife.bind(this, root);
        ls_sensor.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        ls_sensor.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        ls_sensor.setAdapter(new SensorListAdapter(sensors, new RecyclerViewSimpleAdapter.OnItemClickListener<Sensor>() {
            @Override
            public void onClick(View view, int position, Sensor data) {
                loadSensorFragment(data);
            }
        }));
        return root;
    }

    private void loadSensorFragment(Sensor sensor) {
        FragmentManager fm = getActivity().getSupportFragmentManager();

        SensorFragment sensorFragment = (SensorFragment) fm.findFragmentByTag(SensorFragment.class.getSimpleName());
        if(sensorFragment == null){
            sensorFragment = new SensorFragment();
        }
        Bundle bundle = new Bundle();
        bundle.putString("SensorName", sensor.getName());
        bundle.putInt("SensorType", sensor.getType());
        sensorFragment.setArguments(bundle);
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.addToBackStack(null)
                    .replace(R.id.contentLayout,  sensorFragment, SensorFragment.class.getSimpleName())
                    .commit();
    }


    @Override
    public void onDestroyView() {
        Log.d(getClass().getSimpleName(), "onDestroyView method is invoked");
        super.onDestroyView();
        unBinder.unbind();
    }
}
